package desarrolloweb.proyecto.sis.view;


import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.event.map.StateChangeEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.LatLngBounds;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;
import org.springframework.context.annotation.Scope;

import desarrolloweb.proyecto.sis.model.Ubicacion;

@ManagedBean(name = "mapBean")
@Scope("request")
public class MapBean implements Serializable {

	private MapModel emptyModel;
	private MapModel fullModel;
	 
	private String title;
	private String otroNombre;
	private String dir;
	private String tel;
	
	private double lat;	
	private double lng;



	public MapBean() {
		emptyModel = new DefaultMapModel();
		
		fullModel = new DefaultMapModel();  
        
        //Shared coordinates  
        LatLng coord1 = new LatLng(36.879466, 30.667648);  
        LatLng coord2 = new LatLng(36.883707, 30.689216);  
        LatLng coord3 = new LatLng(36.879703, 30.706707);  
        LatLng coord4 = new LatLng(36.885233, 30.702323);  
          
        //Basic marker  
        fullModel.addOverlay(new Marker(coord1, "Konyaalti"));  
        fullModel.addOverlay(new Marker(coord2, "Ataturk Parki"));  
        fullModel.addOverlay(new Marker(coord3, "Karaalioglu Parki"));  
        fullModel.addOverlay(new Marker(coord4, "Kaleici"));  
	}

	public MapModel getEmptyModel() {
		return emptyModel;
	}

	public MapModel getFullModel() {
		return fullModel;
	}
	

	public void addMessage(FacesMessage message) {
		FacesContext.getCurrentInstance().addMessage(null, message);
	}


	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public double getLat() {
		return lat;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public double getLng() {
		return lng;
	}

	public void setLng(double lng) {
		this.lng = lng;
	}

	public void addMarker(ActionEvent actionEvent) {
		Marker marker = new Marker(new LatLng(lat, lng), title);
		emptyModel.addOverlay(marker);

		String linea = 	"Lat:" + lat + 
						", Lng:" + lng+ 
						", Nombre: "+ title +
						", Otro Nombre: " + otroNombre +
						", dir:" + dir + 
						", tel:" + tel;
		
		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Marcador Agregado", linea));
	}


	public String getOtroNombre() {
		return otroNombre;
	}

	public void setOtroNombre(String otroNombre) {
		this.otroNombre = otroNombre;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public void onStateChange(StateChangeEvent event) {  
		LatLngBounds bounds = event.getBounds();  
		int zoomLevel = event.getZoomLevel();  

		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Zoom Level", String.valueOf(zoomLevel)));  
		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Center", bounds.getCenter().toString()));  
		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "NorthEast", bounds.getNorthEast().toString()));  
		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "SouthWest", bounds.getSouthWest().toString()));  
	}  

	public void onPointSelect(PointSelectEvent event) {  
		LatLng latlng = event.getLatLng();  

		addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Point Selected", "Lat:" + latlng.getLat() + ", Lng:" + latlng.getLng()));  
	}  
	
	 
      
   





}
